package com.example.graphic.controller;


import com.example.graphic.AI.AIImageGenerateRequest;
import com.example.graphic.AI.AIImageGenerateResponse;
import com.example.graphic.AI.AIPostersRequest;
import com.example.graphic.model.vo.CommonResponse;
import com.example.graphic.service.AIImageService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * AI图片生成控制器
 */
@Slf4j
@RestController
@RequestMapping("/ai_image")
@Validated
public class AIImageController {
    
    @Autowired
    private AIImageService aiImageService;
    
    /**
     * 生成相似图片
     * @param
     * @return 生成响应
     */
    @PostMapping("/generate")
    public ResponseEntity<CommonResponse<AIImageGenerateResponse>> generateSimilarImages(
           @Valid @RequestBody AIImageGenerateRequest request) {

        log.info("收到模拟AI API表单请求: image={}, prompt={}, num={}",
                request.getImage(),request.getPrompt(),request.getNum());
        
        try {
            // 调用服务层生成图片
            AIImageGenerateResponse response = aiImageService.generateSimilarImages(request);

            if (response.getSuccess()) {
                log.info("图片生成成功，生成数量: {}", response.getGeneratedImageUrls().size());
                return ResponseEntity.ok(CommonResponse.success(response, "图片生成成功"));
            } else {
                log.warn("图片生成失败: {}", response.getMessage());
                return ResponseEntity.status(response.getCode())
                        .body(CommonResponse.error(response.getCode(), response.getMessage()));
            }
            
        } catch (Exception e) {
            log.error("图片生成过程中发生异常", e);
            return ResponseEntity.status(500)
                    .body(CommonResponse.error(500, "系统内部错误，请稍后重试"));
        }
    }



    /**
     * 生成海报
     * @param
     * @return 生成响应
     */
    @PostMapping("/posters")
    public ResponseEntity<CommonResponse<AIImageGenerateResponse>> postersImages(
            @Valid @RequestBody AIPostersRequest request) {

        log.info("收到模拟AI API表单请求: image={}, prompt={}, style={}",
                request.getImage(),request.getPrompt(),request.getStyle());

        try {
            // 调用服务层生成图片
            AIImageGenerateResponse response = aiImageService.postersImages(request);

            if (response.getSuccess()) {
                log.info("图片生成成功，生成数量: {}", response.getGeneratedImageUrls().size());
                return ResponseEntity.ok(CommonResponse.success(response, "图片生成成功"));
            } else {
                log.warn("图片生成失败: {}", response.getMessage());
                return ResponseEntity.status(response.getCode())
                        .body(CommonResponse.error(response.getCode(), response.getMessage()));
            }

        } catch (Exception e) {
            log.error("图片生成过程中发生异常", e);
            return ResponseEntity.status(500)
                    .body(CommonResponse.error(500, "系统内部错误，请稍后重试"));
        }
    }
}